package com.baibei.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.content.res.ResourcesCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.util.AttributeSet;

import com.rae.swift.Rx;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Created by shenminjie on 2017/8/10.
 * mail:shenminjie@100bei.com
 */

public class PriceTextView extends android.support.v7.widget.AppCompatTextView {


    private boolean mEnableRose = true;
    private String mPreFix;

    public PriceTextView(Context context) {
        super(context);
    }

    public PriceTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
    }

    public PriceTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PriceTextView);
        int count = a.getIndexCount();
        for (int i = 0; i < count; i++) {
            int index = a.getIndex(i);
            if (index == R.styleable.PriceTextView_prefix) {
                mPreFix = a.getString(index);
            } else if (index == R.styleable.PriceTextView_enableRose) {
                mEnableRose = a.getBoolean(index, mEnableRose);
            }
        }
        a.recycle();
    }


    public void setPrice(double count) {
        StringBuilder value = new StringBuilder();
        int colorResource;
        String prefix;
        if (count > 0) {
            prefix = "+";
            colorResource = R.color.quotationIncreasingPrimary;
        } else if (count < 0) {
            prefix = "-";
            colorResource = R.color.quotationDecreasingPrimary;
        } else {
            prefix = "";
            colorResource = R.color.quotationNeutralPrimary;
        }
        if (!TextUtils.isEmpty(mPreFix)) {
            value.append(mPreFix);
        }
        if (mEnableRose) {
            value.append(prefix);
        }
        value.append(Rx.formatPrice(Math.abs(count)));
        setText(value);
        setTextColor(ResourcesCompat.getColor(getResources(), colorResource, null));
    }

    /**
     * 设置文本
     *
     * @param price   price
     * @param precent precent
     */
    public void setPriceWithPrecent(double price, double precent) {
        StringBuilder priceString = new StringBuilder();
        //保留小数点，取消四舍五入
        String precentText = new BigDecimal(precent * 100).setScale(2, RoundingMode.DOWN) + "%";
        int colorResource;
        if (price > 0) {
            colorResource = R.color.quotationIncreasingPrimary;
            priceString.append("+");
        } else if (price < 0) {
            colorResource = R.color.quotationDecreasingPrimary;
        } else {
            colorResource = R.color.quotationNeutralPrimary;
        }
        priceString.append(Rx.formatPrice(price));
        priceString.append("  ");
        SpannableString spannableString = new SpannableString(priceString + precentText);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), colorResource, null));
        spannableString.setSpan(colorSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(0.5f);
        spannableString.setSpan(relativeSizeSpan, priceString.length() - 1, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        setText(spannableString);
    }

}
